//
//  Date.swift
//  MedicalBeauty
//
//  Created by Daniel on 2018/4/10.
//  Copyright © 2018年 shen_h. All rights reserved.
//

import Foundation

extension Date {
    
    //该时间所在周的第一天日期（2017年12月17日 00:00:00）
    var startOfWeek: Date {
        var calendar = NSCalendar.current
        calendar.firstWeekday = 2
        let components = calendar.dateComponents(
            Set<Calendar.Component>([.yearForWeekOfYear, .weekOfYear]), from: self)
        return calendar.date(from: components)!.passDate()
    }
    
    //该时间所在周的最后一天日期（2017年12月23日 00:00:00）
    var endOfWeek: Date {
        var calendar = NSCalendar.current
        calendar.firstWeekday = 2
        var components = DateComponents()
        components.day = 6
        return calendar.date(byAdding: components, to: self.startOfWeek)!.passDate()
    }
    
    
    /// date 格式化
    ///
    /// - Parameter string: 格式
    /// - Returns: 格式化的字符串
    func dateNowAsString(_ string:String ) -> String {
        
        let nowDate = self
        let timeZone = TimeZone.init(identifier: "UTC")
        let formatter = DateFormatter()
        formatter.timeZone = timeZone
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = string
        return formatter.string(from: nowDate)
    }
    
    // MARK: 转换为当前时区的时间
    func passDate() -> Date{
        let zone = NSTimeZone.system
        let time:Int = zone.secondsFromGMT(for: self)
        let nowDate = self.addingTimeInterval(TimeInterval(time))
        return nowDate
    }
    
    // MARK:当月第一天
    func startOfCurrentMonth() -> Date {
        let calendar = NSCalendar.current
        let components = calendar.dateComponents(
            Set<Calendar.Component>([.year, .month]), from: self)
        let startOfMonth = calendar.date(from: components)!
        return startOfMonth.passDate()
    }
    
    //月结束日期 date :  月份开始时间
    func endOfCurrentMonth(returnEndTime:Bool = false) -> Date {
        let calendar = NSCalendar.current
        var components = DateComponents()
        components.month = 1
        if returnEndTime {
            components.second = -1
        } else {
            components.day = -1
        }
        
        let endOfMonth =  calendar.date(byAdding: components, to: self)!
        return endOfMonth.passDate()
    }
    
    
    ///  获取当前时间的: 前一周(day:-7)丶前一个月(month:-30)丶前一年(year:-1)的时间戳
    func getExpectTimestamp(year:Int? = nil,month:Int? = nil,day:Int? = nil)->Date{

        let calendar = Calendar.init(identifier: .gregorian)
        var comps:DateComponents = DateComponents()
        comps.year = year == nil ? 0 : year
        comps.month = month == nil ? 0 : month
        comps.day = day == nil ? 0 : day
        return calendar.date(byAdding: comps, to: self)!
    }
    
//    var currentDayzeroOfDate:NSDate{
    
//        let calendar:Calendar = Calendar.current

        
//        var components = calendar.dateComponents(NSCalendar.Unit.year.rawValue|NSCalendar.Unit.month.rawValue|NSCalendar.Unit.day.rawValue, from: self)//NSDateComponents() 不初始化, 直接返回解析的时间
        
//        components.hour = 0
//        components.minute = 0
//        components.second = 0
//        print(" 2  \(components.year)  \(components.month)  \(components.day) \( components.hour)")
//        let date = calendar.date(from: components)
//
//        return date! as NSDate
//    }
    
    var currentDayzeroOfDate:Date{
        
        let calendar:NSCalendar = NSCalendar.current as NSCalendar
        //calendar.components(NSCalendarUnit(), fromDate: self)//UIntMax
        
        let unitFlags: NSCalendar.Unit = [
            NSCalendar.Unit.year,
            NSCalendar.Unit.month,
            NSCalendar.Unit.day,]
        
        //calendar.components(unitFlags, fromDate: self)//解析当前的时间 返回NSDateComponents 解析后的数据后面设置解析后的时间在反转
        var components = calendar.components(unitFlags, from: self)//NSDateComponents() 不初始化, 直接返回解析的时间
        
        components.hour = 0
        components.minute = 0
        components.second = 0
        let date = calendar.date(from: components)
        
        return date!
    }
}
